package com.example.sunmiv1.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.OnlineOrderBean;
import com.example.sunmiv1.entity.OrderDetailInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收銀員權限碼彈窗
 */

public class OrderDetailEditDialog extends BaseDialog {


    @BindView(R.id.orderDetailEdit_off)
    ImageView orderDetailEditOff;
    @BindView(R.id.orderDetailEdit_search)
    ImageView orderDetailEditSearch;
    @BindView(R.id.orderDetailEdit_edit)
    EditText orderDetailEditEdit;
    @BindView(R.id.orderDetailEdit_scan)
    ImageView orderDetailEditScan;
    @BindView(R.id.orderDetailEdit_code)
    TextView orderDetailEditCode;
    @BindView(R.id.orderDetailEdit_code1)
    TextView orderDetailEditCode1;
    @BindView(R.id.orderDetailEdit_code2)
    TextView orderDetailEditCode2;
    @BindView(R.id.orderDetailEdit_confirm)
    TextView orderDetailEditConfirm;

    private Context context;
    private LoadingDialog loadingDialog;
    private OnlineOrderBean.OrderBean.GoodInfoBean bean;
    private OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean bean1;
    private ArrayList<String> judgeList = new ArrayList<>();
    private List<OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean> serialList = new ArrayList<>();

    public static OrderDetailEditDialog newInstance(OnlineOrderBean.OrderBean.GoodInfoBean bean,
                                                    OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean bean1, ArrayList<String> judgeList) {
        OrderDetailEditDialog fragment = new OrderDetailEditDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean", bean);
        bundle.putSerializable("bean1", bean1);
        bundle.putStringArrayList("list", judgeList);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        context = getActivity();
        loadingDialog = new LoadingDialog(context);
        return R.layout.dialog_order_detail_edit;
    }

    @Override
    protected void initData() {
        bean = (OnlineOrderBean.OrderBean.GoodInfoBean) getArguments().getSerializable("bean");
        bean1 = (OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean) getArguments().getSerializable("bean1");
        judgeList = getArguments().getStringArrayList("list");
    }


    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            // 下面這些設置必須在此方法(onStart())中才有效
            Window window = dialog.getWindow();
            // 如果不設置這句代碼, 那麼彈框就會與四邊都有一定的距離
            window.setBackgroundDrawableResource(android.R.color.transparent);
            window.setWindowAnimations(R.style.DialogFromBottom);// 設置動畫
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = dp2px(300);
            window.setAttributes(setLayout(params));
        }
    }


    @OnClick({R.id.orderDetailEdit_off, R.id.orderDetailEdit_search, R.id.orderDetailEdit_scan, R.id.orderDetailEdit_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.orderDetailEdit_off:
                dismiss();
                break;
            case R.id.orderDetailEdit_search:
                if (TextUtils.isEmpty(orderDetailEditEdit.getText().toString().trim())) {
                    Toast.makeText(context, "請輸入要搜索的串碼", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (judgeList.indexOf(orderDetailEditEdit.getText().toString().trim()) != -1) {
                    Toast.makeText(context, "串碼重複", Toast.LENGTH_SHORT).show();
                    return;
                }
                getDeliverySerial();
                break;
            case R.id.orderDetailEdit_scan:
                break;
            case R.id.orderDetailEdit_confirm:
                if (serialList.size() == 0) {
                    Toast.makeText(context, "請先查詢商品串碼", Toast.LENGTH_SHORT).show();
                    return;
                }
                serialModify();
                break;
        }
    }

    /**
     * 27 配送商品搜索串碼
     */
    private void getDeliverySerial() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("goods_id", bean.getGoods_id());
            jsonObject.put("sku_id", bean.getSku_id());
            jsonObject.put("serial_number", orderDetailEditEdit.getText().toString().trim());
            HttpUtils.post(context, Common.SEARCH_SERIAL, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        JSONObject json = new JSONObject(text);
                        if (json.getInt("status") == 1) {
                            JSONObject data = json.getJSONObject("data");
                            serialList.clear();
                            serialList.add(new OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean(
                                    data.getString("warehouse_goods_id"),
                                    data.getString("main_serial_number"),
                                    data.getString("auxiliary_serial_number_one"),
                                    data.getString("auxiliary_serial_number_two")
                            ));
                            orderDetailEditCode.setText(data.getString("main_serial_number"));
                            orderDetailEditCode1.setText(data.getString("auxiliary_serial_number_one"));
                            orderDetailEditCode2.setText(data.getString("auxiliary_serial_number_two"));

                            judgeList.add(data.getString("main_serial_number"));
                            if (!TextUtils.isEmpty(data.getString("auxiliary_serial_number_one"))) {
                                judgeList.add(data.getString("auxiliary_serial_number_one"));
                            }
                            if (!TextUtils.isEmpty(data.getString("auxiliary_serial_number_two"))) {
                                judgeList.add(data.getString("auxiliary_serial_number_two"));
                            }
                        } else {
                            Toast.makeText(context, json.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void serialModify() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", bean1.getId());
            jsonObject.put("warehouse_goods_id", serialList.get(0).getId());
            HttpUtils.post(context, Common.SERIAL_MODIFY, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        JSONObject json = new JSONObject(text);
                        Toast.makeText(context, json.getString("message"), Toast.LENGTH_SHORT).show();
                        if (json.getInt("status") == 1) {
                            bean1.setId(serialList.get(0).getId());
                            bean1.setMain_serial_number(serialList.get(0).getMain_serial_number());
                            bean1.setAuxiliary_serial_number_one(serialList.get(0).getAuxiliary_serial_number_one());
                            bean1.setAuxiliary_serial_number_two(serialList.get(0).getAuxiliary_serial_number_two());
                            callback.success(bean1);
                            dismiss();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
